package com.tyc.es.utills;

import com.tyc.es.model.Product;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: java类作用描述
 * @Author: tyc
 * @CreateDate: 2021/1/7 11:39
 */
@Component
public class HTMLParseUtill {
    String url = "https://list.tmall.com/search_product.htm?q=%CA%E9%BC%AE&type=p&vmarket=&spm=875.7931836%2FB.a2227oh.d100&from=mallfp..pc_1_searchbutton";

    public String getHtml()throws Exception{
        //打开浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String html = "";
        //2.创建get请求，相当于在浏览器地址栏输入 网址
        HttpGet request = new HttpGet(url);
        //3.执行get请求，相当于在输入地址栏后敲回车键
        response = httpClient.execute(request);

        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            HttpEntity httpEntity = response.getEntity();
            html = EntityUtils.toString(httpEntity, "utf-8");
        }
        return html;
    }

    public List<Product> parse() throws Exception {
        String html = getHtml();
        if(!StringUtils.hasText(html)){
            return null;
        }
        Document document = Jsoup.parse(html);
        Element j_itemList = document.getElementById("J_ItemList");
        Elements product = j_itemList.getElementsByClass("product");
        List<Product> products = new ArrayList<>();
        for (Element el:product){
            Elements elementsByClass = el.getElementsByClass("product-iWrap");
            String price = elementsByClass.select("p[class=productPrice]").text();
            price = price.substring(1,price.length()-1);
            String img = elementsByClass.select("div[class=productImg-wrap] a").attr("href");
            String name = elementsByClass.select(".productTitle a").text();
            String shopName = elementsByClass.select(".productShop a").text();
            String shopUrl = elementsByClass.select(".productShop a").attr("href");
            Elements spans = elementsByClass.select(".productStatus  span");
            String saleNum = spans.get(0).select("em").text();
            saleNum=saleNum.substring(0,saleNum.length()-1);
            String commentNum = spans.get(1).select("a").text();
            Product p = new Product(img,name,price,shopName,shopUrl,Integer.parseInt(saleNum),Integer.parseInt(commentNum));
            products.add(p);
        }
        return products;
    }

}
